package databasetest;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;

import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.border.EmptyBorder;

public class DatabaseTest extends JFrame {

	static final String driver = "oracle.jdbc.driver.OracleDriver";
	static final String url = "jdbc:oracle:thin:@127.0.0.1:1521:XE";
	static final String id = "hr";
	static final String password = "hr";
	Connection conn;
//쿄쿄쿄
	JDesktopPane contentPane;
	private JMenuBar menuBar;
	private JMenu mnNewMenu;
	private JMenuItem mntmNewMenuItem;
	private JMenuItem mntmNewMenuItem_1;
	private JMenuItem mntmNewMenuItem_2;
	private JMenuItem mntmNewMenuItem_3;

	Input input;
	Update update;
	Delete delete;
	Search search;

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					DatabaseTest frame = new DatabaseTest();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public DatabaseTest() {
		setTitle("데이터베이스 연습");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 1061, 543);
		setJMenuBar(getMenuBar_1());
		contentPane = new JDesktopPane();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		// 데이터베이스 연결 테스트
		// 1) 드라이버
		try {
			Class.forName(driver).newInstance();
			System.out.println("드라이버가 로딩되었습니다.");

			conn = DriverManager.getConnection(url, "hr", "hr");
			System.out.println("데이터베이스가 정상적으로 연결됨");
		} catch (Exception ev) {
			ev.getMessage();
		}
	}

	public void openInput() {
		if (input == null) {
			input = new Input(conn, this);
			contentPane.add(input);
		}
		input.setVisible(true);
		input.toFront();
	}

	public void openDelete() {
		if (delete == null) {
			delete = new Delete(conn, this);
			contentPane.add(delete);

		}
		delete.setVisible(true);
		delete.toFront();
	}

	public void openUpdate() {
		if (update == null) {
			update = new Update(conn, this);
			contentPane.add(update);
		} else if (update.isVisible() == false) {
			contentPane.add(update);
		}
		update.toFront();
		update.setVisible(true);

	}

	public void openSearch() {

		if (search == null) {
			search = new Search(conn, this);
			contentPane.add(search);
		} else if (search.isVisible() == false)
			contentPane.add(search);
		search.setVisible(true);
	}

	private JMenuBar getMenuBar_1() {
		if (menuBar == null) {
			menuBar = new JMenuBar();
			menuBar.add(getMnNewMenu());
		}
		return menuBar;
	}

	private JMenu getMnNewMenu() {
		if (mnNewMenu == null) {
			mnNewMenu = new JMenu("New menu");
			mnNewMenu.add(getMntmNewMenuItem());
			mnNewMenu.add(getMntmNewMenuItem_2());
			mnNewMenu.add(getMntmNewMenuItem_1());
			mnNewMenu.add(getMntmNewMenuItem_3());
		}
		return mnNewMenu;
	}

	private JMenuItem getMntmNewMenuItem() {
		if (mntmNewMenuItem == null) {
			mntmNewMenuItem = new JMenuItem("입력");
			mntmNewMenuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					openInput();
				}
			});
		}
		return mntmNewMenuItem;
	}

	private JMenuItem getMntmNewMenuItem_1() {
		if (mntmNewMenuItem_1 == null) {
			mntmNewMenuItem_1 = new JMenuItem("삭제");
			mntmNewMenuItem_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					openDelete();
				}
			});
		}
		return mntmNewMenuItem_1;
	}

	private JMenuItem getMntmNewMenuItem_2() {
		if (mntmNewMenuItem_2 == null) {
			mntmNewMenuItem_2 = new JMenuItem("수정");
			mntmNewMenuItem_2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					openUpdate();
				}
			});
		}
		return mntmNewMenuItem_2;
	}

	private JMenuItem getMntmNewMenuItem_3() {
		if (mntmNewMenuItem_3 == null) {
			mntmNewMenuItem_3 = new JMenuItem("조회");
			mntmNewMenuItem_3.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					openSearch();
				}
			});
		}
		return mntmNewMenuItem_3;
	}
}